package com.power.using.askapp.view;

import android.app.Dialog;
import android.content.Context;
import android.util.Log;
import android.view.Display;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.power.using.askapp.R;


/**
 * 项目名称:YZDSuperviseP
 * 创建人:zhangtainjia
 * 创建时间:2017/3/21 16:25
 */

public class SelectDialogView {
    TextView mTxtTitle;
    TextView mTxtMsg;
    Button mBtnNeg;
    Button mBtnPos;
    EditText mEditPsw;
    TextView mWrongDialog;
    LinearLayout mLLayoutBg;
    private Display mDisplay;
    public static Dialog mDialog;
    private Context mContext;
    private View mView;
    private String mCardID;
    private String mCert_id;

    public SelectDialogView(Context context) {

        this.mContext = context;


        WindowManager windowManager = (WindowManager) context
                .getSystemService(Context.WINDOW_SERVICE);
        mDisplay = windowManager.getDefaultDisplay();

    }
    public  void createLoadingDialog(Context context, String msg) {
        LayoutInflater inflater = LayoutInflater.from(context);
        View v = inflater.inflate(R.layout.loadingdialog, null);// 得到加载view

        TextView tipTextView = (TextView) v.findViewById(R.id.id_tv_loadingmsg);// 提示文字
        tipTextView.setText(msg);// 设置加载信息

        mDialog= new Dialog(context, R.style.AlertDialogStyle);// 创建自定义样式dialog
        mDialog.setCancelable(true); // 是否可以按“返回键”消失
        mDialog.setCanceledOnTouchOutside(false); // 点击加载框以外的区域
        mDialog.setContentView(v, new LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.MATCH_PARENT,
                LinearLayout.LayoutParams.MATCH_PARENT));// 设置布局
        /** *将显示Dialog的方法封装在这里面 */
        Window window = mDialog.getWindow();
        WindowManager.LayoutParams lp = window.getAttributes();
        lp.width = WindowManager.LayoutParams.MATCH_PARENT;
        lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
        window.setGravity(Gravity.CENTER);
        window.setAttributes(lp);
        window.setWindowAnimations(R.style.PopWindowAnimStyle);
        mDialog.show();


    }

    public SelectDialogView builderEidt() {
        mView = LayoutInflater.from(mContext).inflate(R.layout.edit_view_alertdialog, null);

        mTxtTitle= (TextView) mView.findViewById(R.id.txt_title);
        mTxtMsg= (TextView) mView.findViewById(R.id.txt_msg);
        mBtnNeg= (Button) mView.findViewById(R.id.btn_neg);
        mBtnPos= (Button) mView.findViewById(R.id.btn_pos);
        mEditPsw= (EditText) mView.findViewById(R.id.edit_psw);
        mWrongDialog= (TextView) mView.findViewById(R.id.wrong_dialog);
        mWrongDialog.setVisibility(View.GONE);
        mLLayoutBg= (LinearLayout) mView.findViewById(R.id.lLayout_bg);
        // 定义Dialog布局和参数
        mDialog = new Dialog(mContext, R.style.AlertDialogStyle);
        mDialog.setContentView(mView);


        // 调整dialog背景大小
        mLLayoutBg.setLayoutParams(new FrameLayout.LayoutParams((int) (mDisplay
                .getWidth() * 0.85), LinearLayout.LayoutParams.WRAP_CONTENT));
        return this;
    }
    public Dialog mDialog(){
        return mDialog;
    }
    public EditText getEdit_psw() {
        return mEditPsw;
    }
    public TextView getWrongTextView(){
        return mWrongDialog;
    }
    public SelectDialogView setWrong(String wrong){
        mWrongDialog.setVisibility(View.VISIBLE);
        if (wrong.equals("")){
            mWrongDialog.setText("");

        }else{
            mWrongDialog.setText(wrong);
        }
        return  this;
    }
    public SelectDialogView setTitle(String title) {

        if ("".equals(title)) {
            mTxtTitle.setText("标题");
        } else {
            mTxtTitle.setText(title);
        }
        return this;
    }

    public SelectDialogView setMsg(String msg) {

        if ("".equals(msg)) {
            mTxtMsg.setText("内容");
        } else {
            mTxtMsg.setText(msg);
        }
        return this;
    }
    public SelectDialogView setPositiveButton(String text,
                                            final View.OnClickListener listener) {

        if ("".equals(text)) {
            mBtnPos.setText("确定");
        } else {
            mBtnPos.setText(text);
        }
        mBtnPos.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {


                listener.onClick(v);


            }


        });
        return this;
    }

    public SelectDialogView setNegativeButton(String text,
                                            final View.OnClickListener listener) {

        if ("".equals(text)) {
            mBtnNeg.setText("取消");
        } else {
            mBtnNeg.setText(text);
        }
        mBtnNeg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                listener.onClick(v);
                mDialog.dismiss();
            }
        });
        return this;
    }
    public static void cancle(){
        if (mDialog != null && mDialog.isShowing()) {
            try {
                mDialog.dismiss();
            } catch (Exception e) {
                Log.e("hashiqi","progressDialog销毁失败");
            }
        }
        mDialog = null;
    }
    public void show() {

        mDialog.show();
    }

    public SelectDialogView setViewGone(){
        mEditPsw.setVisibility(View.GONE);
        return this;
    }

    public SelectDialogView setOutClick(boolean isOut){
        if (mDialog != null) {
            mDialog.setCanceledOnTouchOutside(isOut);
        }
        return this;
    }

    public boolean isShowing(){
       return mDialog.isShowing();
    }




}
